package com.easymin.weixin.manager.impl;

import com.easymin.weixin.common.DataForm;
import com.easymin.weixin.entity.AllowAccessIp;
import com.easymin.weixin.manager.AllowAccessIpManager;
import com.easymin.weixin.service.AllowAccessIpService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class DefaultAllowAccessIpManager implements AllowAccessIpManager {

  @Autowired
  private AllowAccessIpService accessIpService;

  @Override
  public AllowAccessIp find() {

    return accessIpService.fetch(1L);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public DataForm<AllowAccessIp> update(String content) {
    AllowAccessIp allowAccessIp = new AllowAccessIp();
    allowAccessIp.setContent(content);
    allowAccessIp.setId(1);
    long count = accessIpService.count();
    if (count == 0) {
      allowAccessIp.setCreated(new Date());
      allowAccessIp.setUpdated(new Date());
      accessIpService.insert(allowAccessIp);
    } else {
      allowAccessIp.setUpdated(new Date());
      accessIpService.updateAll(content);
    }
    DataForm<AllowAccessIp> dataForm = new DataForm<AllowAccessIp>();

    dataForm.setStatus(0);
    dataForm.setMessage("成功");

    return dataForm;
  }

}
